package cz.possoft.explorer.resolver;

import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;

import javax.management.MBeanServer;
import javax.management.ObjectName;

/**
 * Resolver for ObjectName type, which can be used to work with MBeanServer
 * 
 * TODO: This is useless at the moment. We need to add some useful functionality to work with MBeanServer. 
 * 
 * @author <a href="mailto:mposolda@gmail.com">Marek Posolda</a>
 * @version $Revision$
 */
public class ObjectNameResolverImpl extends GetterNamesResolverImpl<ObjectName>
{
   private static MBeanServer mbeanServer;
   
   // inject mbean server during class initialization
   static 
   {
      injectMBeanServer();
      System.out.println("MBean Server: " + mbeanServer);            
   }

   
   private static void injectMBeanServer()
   {
      try
      {
         Class<?> clazz =
            Thread.currentThread().getContextClassLoader().loadClass("org.jboss.mx.util.MBeanServerLocator");
         Method m = clazz.getMethod("locateJBoss");
         mbeanServer = (MBeanServer)m.invoke(null);
      }
      catch (Exception ignore)
      {
         System.err.println("Can't locate MBean Server via JBoss MBeanServerLocator. Trying generic way.");
         ignore.printStackTrace();
         
         mbeanServer = ManagementFactory.getPlatformMBeanServer();
      }
   }
}

